﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace Dungeon_Dive
{
    /// <summary>
    /// Simple menu.
    /// </summary>
    public class Menu : DrawableGameComponent
    {
        Game parentGame;
        SpriteBatch sb;
        Log log;

        SpriteFont Font { get; set; }
        int spacing;
        int width;        

        public Vector2 MenuPosition { get; set; }
               
        List<String> items;
        int current;

        public Menu(Game game)
            : base(game)
        {
            parentGame = game;
            Font = parentGame.Content.Load<SpriteFont>("Fonts\\Debugfont");
            spacing = Font.LineSpacing;
            
            log = (Log)game.Services.GetService(typeof(Log));

            MenuPosition = new Vector2((float)Game.Window.ClientBounds.Width/2,
                                        (float)Game.Window.ClientBounds.Height/2);            

            items = new List<String>();           
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            log.Write("Menu.Init()");
            base.Initialize();
        }

        protected override void LoadContent()
        {
            log.Write("Menu.LoadContent()");
            ContentManager contentManager = (ContentManager)parentGame.Services.GetService(typeof(ContentManager));
            sb = contentManager.SpriteBatch;
            if (sb == null) log.Write("Menu unable to get SpriteBatch");

            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Vector2 pos = MenuPosition;

            if (sb == null)
                log.Write("Menu has no SpriteBatch");            

            sb.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront,
                SaveStateMode.SaveState);
            foreach (string s in items)
            {
                if (items.IndexOf(s) == current)
                    sb.DrawString(Font, s, pos, Color.Wheat,
                                    0.0f, new Vector2(0, 0), 1.2f, SpriteEffects.None,0);
                else
                    sb.DrawString(Font, s, pos, Color.White);
                pos.Y += spacing;
            }
            sb.End();
            base.Draw(gameTime);
        }

        public void AddItem(string text)
        {
            items.Add(text);
            Vector2 size = Font.MeasureString(text);
            if (size.X > width)
                width = (int)size.X;
        }

        public void Up()
        {
            current--;
            if (current < 0)
                current = items.Count-1;
        }

        public void Down()
        {
            current++;
            if (current > items.Count - 1)
                current = 0;
        }
        public int GetSelected()
        {
            if (items.Count > 0)
                return current;
            else
                return -1;
        }

    }
}